package userHelp

import (
	myredis "common/redis"
	"github.com/shopspring/decimal"
)

type TblMemberBalance struct {
	Prefix            string  `json:"prefix" db:"prefix" cbor:"prefix"`
	Uid               string  `json:"uid" db:"uid" cbor:"uid"`
	Brl               float64 `json:"brl" db:"brl" cbor:"brl"`
	LockAmount        float64 `json:"lock_amount" db:"lock_amount" cbor:"lockAmount"`                        //提现中余额//全部余额
	UnlockAmount      float64 `json:"unlock_amount" db:"unlock_amount" cbor:"unlock_amount"`                 //可提额度
	AgencyAmount      float64 `json:"agency_amount" db:"agency_amount" cbor:"agencyAmount"`                  //临时额度
	DepositLockAmount float64 `json:"deposit_lock_amount" db:"deposit_lock_amount" cbor:"depositLockAmount"` //存款不可提现余额
	AgencyLockAmount  float64 `json:"agency_lock_amount" db:"agency_lock_amount" cbor:"agencyLockAmount"`    //推广账户不可提现余额
	SandbagAmount     float64 `json:"sandbag_amount" db:"sandbag_amount" cbor:"sandbag_amount"`
}

var g_tbl_member_balance string = "tbl_member_balance:"

func AddUserSandbagAmount(uid string, SandbagAmount float64) {
	LoadUserToRedis(uid)
	myredis.AddUserFieldValueByFloat64(uid, g_tbl_member_balance+"sandbag_amount", SandbagAmount)
}

func GetUserSandbagAmount(uid string) float64 {
	var fbrl float64
	fbrl = myredis.GetUserFieldFloat64(uid, g_tbl_member_balance+"sandbag_amount")
	return fbrl
}

func GetBalanceByUid(uid string, precision int32) decimal.Decimal {
	var fbrl float64
	fbrl = myredis.GetUserFieldFloat64(uid, g_tbl_member_balance+"brl")
	return decimal.NewFromFloat(fbrl).Truncate(precision)
}

func GetBalanceFloat64ByUid(uid string) float64 {
	var fbrl float64
	fbrl = myredis.GetUserFieldFloat64(uid, g_tbl_member_balance+"brl")
	return fbrl
}

func GetBalanceStringByUid(uid string) string {
	return myredis.GetUserFieldString(uid, g_tbl_member_balance+"brl")
}

func AddUserBalanceByFloat64(uid string, AddBalance float64) {
	myredis.AddUserFieldValueByFloat64(uid, g_tbl_member_balance+"brl", AddBalance)
}

func SetUserBalanceByFloat64(uid string, LastBalance float64) {
	myredis.SetUserFieldValueByFloat64(uid, g_tbl_member_balance+"brl", LastBalance)
}

func AddUserBalanceByDecimal(uid string, AddBalance decimal.Decimal) {
	myredis.AddUserFieldValueByDecimal(uid, g_tbl_member_balance+"brl", AddBalance)
}

func MinusUserBalanceByDecimal(uid string, AddBalance decimal.Decimal) {
	myredis.MinusUserFieldValueByDecimal(uid, g_tbl_member_balance+"brl", AddBalance)
}

func GetMemberBalanceInfo(uid string) (TblMemberBalance, bool) {
	tblMemBalanceInfo := TblMemberBalance{}

	LoadUserToRedis(uid)
	if myredis.IsUserExis(string(uid)) {
		tblMemBalanceInfo.Uid = uid
		tblMemBalanceInfo.Prefix = myredis.GetUserFieldString(uid, g_tbl_member_balance+"prefix")
		tblMemBalanceInfo.Brl = myredis.GetUserFieldFloat64(uid, g_tbl_member_balance+"brl")
		tblMemBalanceInfo.UnlockAmount = myredis.GetUserFieldFloat64(uid, g_tbl_member_balance+"unlock_amount")
		tblMemBalanceInfo.AgencyAmount = myredis.GetUserFieldFloat64(uid, g_tbl_member_balance+"agency_amount")
		tblMemBalanceInfo.LockAmount = myredis.GetUserFieldFloat64(uid, g_tbl_member_balance+"lock_amount")
		return tblMemBalanceInfo, true
	}

	return tblMemBalanceInfo, false
}

func GetUnlockAmount(uid string) float64 {
	LoadUserToRedis(uid)
	var fbrl float64
	fbrl = myredis.GetUserFieldFloat64(uid, g_tbl_member_balance+"unlock_amount")
	return fbrl
}

func AddUnlockAmount(uid string, unlockAmount float64) {
	LoadUserToRedis(uid)
	myredis.AddUserFieldValueByFloat64(uid, g_tbl_member_balance+"unlock_amount", unlockAmount)
}

func SetUnlockAmount(uid string, unlockAmount float64) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValueByFloat64(uid, g_tbl_member_balance+"unlock_amount", unlockAmount)
}

func SetAgencyAmount(uid string, AgencyAmount float64) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValueByFloat64(uid, g_tbl_member_balance+"agency_amount", AgencyAmount)
}

func AddAgencyAmount(uid string, AgencyAmount float64) {
	LoadUserToRedis(uid)
	myredis.AddUserFieldValueByFloat64(uid, g_tbl_member_balance+"agency_amount", AgencyAmount)
}

func GetAgencyAmount(uid string) float64 {
	LoadUserToRedis(uid)
	var fbrl float64
	fbrl = myredis.GetUserFieldFloat64(uid, g_tbl_member_balance+"agency_amount")
	return fbrl
}
